/*
 * AS3935.h
 *
 *  Created on: Sep 5, 2020
 *      Author: daniel
 */

#ifndef BSP_LIGHTNING_AS3935_H_
#define BSP_LIGHTNING_AS3935_H_

#include <stdbool.h>
#include "stm32l0xx.h"

enum SF_AS3935_REGISTER_NAMES {

	AFE_GAIN          = 0x00,
	THRESHOLD,
	LIGHTNING_REG,
	INT_MASK_ANT,
	ENERGY_LIGHT_LSB,
	ENERGY_LIGHT_MSB,
	ENERGY_LIGHT_MMSB,
	DISTANCE,
	FREQ_DISP_IRQ,
	CALIB_TRCO        = 0x3A,
	CALIB_SRCO        = 0x3B,
	RESET_LIGHT       = 0x3C,
	CALIB_RCO         = 0x3D

};

// Masks for various registers, there are some redundant values that I kept
// for the sake of clarity.
enum SF_AS3935_REGSTER_MASKS {

  WIPE_ALL          = 0x0,
  INT_MASK          = 0xF,
  ENERGY_MASK       = 0x1F,
  SPI_READ_M        = 0x40,
  CALIB_MASK        = 0x40,
  OSC_MASK          = 0x1F,
  DISTANCE_MASK     = 0x3F,
  DIV_MASK          = 0x3F,
  NOISE_FLOOR_MASK  = 0x8F,
  GAIN_MASK         = 0xC1,
  STAT_MASK         = 0xBF,
  DISTURB_MASK      = 0xDF,
  LIGHT_MASK        = 0xCF,
  SPIKE_MASK        = 0xF0,
  THRESH_MASK       = 0xF0,
  CAP_MASK          = 0xF0,
  POWER_MASK        = 0xFE

};

typedef enum INTERRUPT_STATUS {

  NOISE_TO_HIGH     = 0x01,
  DISTURBER_DETECT  = 0x04,
  LIGHTNING         = 0x08

} lightningStatus;

#define INDOOR            0x12
#define OUTDOOR           0xE

#define DIRECT_COMMAND    0x96
#define UNKNOWN_ERROR     0xFF

bool AS3935_Init();
void AS3935_ISR();
bool AS3935_GetData(enum INTERRUPT_STATUS* type, uint16_t* dist);


#endif /* BSP_LIGHTNING_AS3935_H_ */
